home *** CD-ROM | disk | FTP | other *** search
/ ADA Programming Guide / ADA Programming Guide.iso / ada_gnu / adainc / a-nudira.adb < prev    next >
Text File  |  1996-01-30  |  4KB  |  110 lines

  1. ------------------------------------------------------------------------------
  2. --                                                                          --
  3. --                         GNAT RUNTIME COMPONENTS                          --
  4. --                                                                          --
  5. --         A D A . N U M E R I C S . D I S C R E T E _ R A N D O M          --
  6. --                                                                          --
  7. --                                 B o d y                                  --
  8. --                                                                          --
  9. --                            $Revision: 1.8 $                              --
  10. --                                                                          --
  11. --           Copyright (c) 1992,1993,1994 NYU, All Rights Reserved          --
  12. --                                                                          --
  13. -- The GNAT library is free software; you can redistribute it and/or modify --
  14. -- it under terms of the GNU Library General Public License as published by --
  15. -- the Free Software  Foundation; either version 2, or (at your option) any --
  16. -- later version.  The GNAT library is distributed in the hope that it will --
  17. -- be useful, but WITHOUT ANY WARRANTY;  without even  the implied warranty --
  18. -- of MERCHANTABILITY  or  FITNESS FOR  A PARTICULAR PURPOSE.  See the  GNU --
  19. -- Library  General  Public  License for  more  details.  You  should  have --
  20. -- received  a copy of the GNU  Library  General Public License  along with --
  21. -- the GNAT library;  see the file  COPYING.LIB.  If not, write to the Free --
  22. -- Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.        --
  23. --                                                                          --
  24. ------------------------------------------------------------------------------
  25.  
  26. with Calendar;
  27. with Unchecked_Deallocation;
  28.  
  29. package body Ada.Numerics.Discrete_Random is
  30.  
  31.    --------------
  32.    -- Finalize --
  33.    --------------
  34.  
  35.    procedure Finalize (Gen : in out Generator) is
  36.  
  37.       procedure Destroy_State is
  38.         new Unchecked_Deallocation (ANR.State, Access_State);
  39.  
  40.    begin
  41.       Destroy_State (Gen.State);
  42.    end Finalize;
  43.  
  44.    -----------
  45.    -- Image --
  46.    -----------
  47.  
  48.    function Image (Of_State : State) return String is
  49.    begin
  50.       return ANR.Image (ANR.State (Of_State));
  51.    end Image;
  52.  
  53.    ------------
  54.    -- Random --
  55.    ------------
  56.  
  57.    function Random (Gen : Generator) return Result_Subtype is
  58.       Lo, Hi : Float;
  59.       F      : Float;
  60.       R      : Float;
  61.       U      : Float;
  62.  
  63.    begin
  64.       ANR.Random (Gen.State.all, U);
  65.       Lo := Float (Result_Subtype'Pos (Result_Subtype'First));
  66.       Hi := Float (Result_Subtype'Pos (Result_Subtype'Last));
  67.       R  := (Hi - Lo) * U - 0.5;
  68.  
  69.       return Result_Subtype'Val (Long_Long_Integer (R));
  70.    end Random;
  71.  
  72.    -----------
  73.    -- Reset --
  74.    -----------
  75.  
  76.    procedure Reset (Gen : in Generator; Initiator : in Integer) is
  77.    begin
  78.       ANR.Reset (Gen.State.all, Initiator);
  79.    end Reset;
  80.  
  81.    procedure Reset (Gen : in Generator) is
  82.    begin
  83.       ANR.Reset (Gen.State.all);
  84.    end Reset;
  85.  
  86.    procedure Reset (Gen : in Generator; From_State : in State) is
  87.    begin
  88.       Gen.State.all := ANR.State (From_State);
  89.    end Reset;
  90.  
  91.    ----------
  92.    -- Save --
  93.    ----------
  94.  
  95.    procedure Save (Gen : in Generator; To_State : out State) is
  96.    begin
  97.       To_State := State (Gen.State.all);
  98.    end Save;
  99.  
  100.    -----------
  101.    -- Value --
  102.    -----------
  103.  
  104.    function Value (Coded_State : String) return State is
  105.    begin
  106.       return State (ANR.Value (Coded_State));
  107.    end Value;
  108.  
  109. end Ada.Numerics.Discrete_Random;
  110.